#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>

@interface LocationManager ()<CLLocationManagerDelegate>

//设置一个定位管理者
@property (nonatomic, strong) CLLocationManager *locationManager;
//存储推算出当前位置的地理信息
@property (nonatomic, strong) CLGeocoder *geoCoder;
//获取自身的经纬度坐标
@property (nonatomic, retain) CLLocation *myLocation;

@end

@implementation LocationManager

static id _instace;

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instace = [[self alloc] init];
    });
    return _instace;
}


//开始定位
- (void)startLocation{
    [self.locationManager startUpdatingLocation];
}


//停止定位
- (void)stopLocation{
    [self.locationManager stopUpdatingLocation];
}


- (CLLocationManager *)locationManager{
    if (!_locationManager) {
        //初始化定位管理者
        _locationManager = [[CLLocationManager alloc] init];
        //判断设备是否能够进行定位
        if ([CLLocationManager locationServicesEnabled]) {
            _locationManager.delegate = self;
            //精确度获取到米
            _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            //设置过滤器为无
            _locationManager.distanceFilter = kCLDistanceFilterNone;
            // 取得定位权限，有两个方法，取决于你的定位使用情况
            //一个是requestAlwaysAuthorization，一个是requestWhenInUseAuthorization
            [_locationManager requestWhenInUseAuthorization];
            //地理信息
            _geoCoder = [[CLGeocoder alloc] init];
        }
    }
    return  _locationManager;
}

//设置获取位置信息的代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    self.myLocation = locations.lastObject;
    
    if (_getLocationBlock) {
        _getLocationBlock(_myLocation);
    }
    
    //通过坐标来推算该位置的相关信息
//    [self.geoCoder reverseGeocodeLocation:self.myLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//        if (placemarks.count > 0) {
//            CLPlacemark *placemark = [placemarks objectAtIndex:0];
//            NSLog(@"%@", placemark.name);
//            //获取城市
//            NSString *city = placemark.locality;
//            if (!city) {
//                //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
//                city = placemark.administrativeArea;
//            }
//            // 位置名
//            NSLog(@"name, %@", placemark.name);
//            // 街道
//            NSLog(@"thoroughfare, %@", placemark.thoroughfare);
//            // 子街道
//            NSLog(@"subThoroughfare, %@", placemark.subThoroughfare);
//            // 市
//            NSLog(@"locality, %@", placemark.locality);
//            // 区
//            NSLog(@"subLocality, %@", placemark.subLocality);
//            // 国家
//            NSLog(@"country, %@", placemark.country);
//        } else if (error == nil && [placemarks count] == 0) {
//            NSLog(@"No results were returned.");
//        } else if (error != nil) {
//            NSLog(@"An error occurred = %@", error);
//        }
//    }];
    //不用的时候关闭更新位置服务，不关闭的话这个 delegate 隔一定的时间间隔就会有回调
    [self.locationManager stopUpdatingLocation];
}

//获取位置信息出错时
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if (error) {
        //        NSLog(@"error:%ld", (long)error.code);
    }
}

@end
